var prefix = api + "/msg/messages";
var root;
var evt;
$(function () {
  var msgTypeDatas = dictByType('message_type')
  var readStatusDatas = dictByType('msg_status')
  renderDictFilter('#status', 'msg_status')
  renderDictFilter('#msgType', 'message_type')
  String.prototype.trim = function () {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  }
  if (document.createEvent) {
    evt = document.createEvent("HTMLEvents");
  }

  $('#detail-frame').css('display', 'none')
//	var webMsgId = localStorage.getItem("websocketMsgId")
//	if(webMsgId){
//		readDetail(webMsgId)
//		localStorage.removeItem("websocketMsgId")
//	}

  root = window.parent.document
  var options = {
    url: prefix + "/page/received",
    modalName: "我的消息",
    columns: [
      {
        field: 'msgTitle',
        title: '消息标题'
      },
      {
        field: 'msgType',
        title: '消息类型',
        formatter: function (value, row, index) {
          return $.table.selectDictLabel(msgTypeDatas, value);
        }
      },
      {
        field: 'receiverReadStatus',
        title: '状态',
        formatter: function (value, row, index) {
          return $.table.selectDictLabel(readStatusDatas, value);
        }
      },
      {
        field: 'createTime',
        title: '创建时间'
      },
      {
        title: '操作',
        align: 'center',
        formatter: function (value, row, index) {
          var actions = [];
          actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="readDetail(\'' + row.msgId + '\')"><i class="fa fa-edit"></i>查看详情</a> ');
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

function readDetail(msgId) {
  $('#detail-frame').css('display', 'block')
  readMessage(msgId)
  loadDetailById(msgId, function (response) {
    $.table.refresh()
    if (response.code === 0) {
      var message = response.data
      if (message.msgTitle != null && message.msgTitle != 'undefined') {
        $("#title").text(message.msgTitle.trim())
      }
      if (message.createTime != null && message.createTime != 'undefined') {
        $("#createTime").text(message.createTime)
      }
      if (message.msgContent != null && message.msgContent != 'undefined') {
        $("#content").html(message.msgContent)
      }
      var width, height;
      if ($.common.isEmpty(width)) {
        width = 800;
      }
      if ($.common.isEmpty(height)) {
        height = ($(window).height() - 50);
      }
      layer.open({
        type: 1,
        maxmin: true,
        title: message.msgTitle.trim(),
        area: [width + 'px', height + 'px'],
        content: $('#detail-frame'),
        end: function () {
          $("#title").text('')
          $("#createTime").text('')
          $("#content").html('')
          $('#detail-frame').css('display', 'none')
        }
      })
    }
  })
}

function loadDetailById(msgId, callback) {
  var option = {
    url: prefix + "/received/" + msgId,
    method: "get",
    dataType: 'json',
    success: callback
  }
  $.ajax(option)
}

function readMessage(msgId) {
  var option = {
    url: prefix + "/read/" + msgId,
    method: "put",
    dataType: 'json',
    success: function (response) {
      if (response.code === 0) {
        if (document.createEvent) {
          evt.initEvent("receiveMessage", true, false);
          root.dispatchEvent(evt)
        } else {
          window.parent.receiveMessage()
        }

      }
    }
  }
  $.ajax(option)
}